
<?php
/**
 *数据库类
 */
class Db{
	private static $instance = null;
	private static $mysqli = null;
	private $db_host;
	private $db_user;
	private $db_psw;
	private $db_name;
	private $db_charset;
	public static function connect(){
		if(is_null(self::$instance)){
			self::$instance = new self();
		}
		return self::$instance;
	}

	public function query($query){
		return self::$mysqli->query($query);
	}

	public function prepare($query){
		return self::$mysqli->prepare($query);
	}

	public function execute($query){
		return self::$mysqli->execute($query);
	}

	public function &getMysqli(){
		return self::$mysqli;
	}

	protected function __construct(){
		$this->db_host = C('env.db.host');
		$this->db_user = C('env.db.user');
		$this->db_psw = C('env.db.psw');
		$this->db_name = C('env.db.name');
		$this->db_charset = C('env.db.charset');
		if(is_null(self::$mysqli)){
			self::$mysqli = new mysqli($this->db_host, 
									   $this->db_user,
									   $this->db_psw,
									   $this->db_name);
			if(self::$mysqli->connect_errno){
				die('CONNECT TO DB FAILED');
			}
			self::$mysqli->set_charset($this->db_charset);
		}
	}

	public function __destruct(){
		if(!is_null(self::$mysqli)){
			self::$mysqli->close();
		}
	}

	protected function __clone(){}
}